package com.kingschan.jeasytag.tag.power;

import java.io.File;
import java.io.IOException;
import javax.servlet.jsp.JspException;

import com.kingschan.jeasytag.container.EasyTagContainer;
import com.kingschan.jeasytag.container.impl.EasyTagContainerImpl;
import com.kingschan.jeasytag.core.annotations.tag.PropertyField;
import com.kingschan.jeasytag.core.annotations.tag.StringField;
import com.kingschan.jeasytag.tag.RootTag;
import com.kingschan.jeasytag.tag.grid.GridTag;
import com.kingschan.util.bean.BeanUtil;
import com.kingschan.util.encryption.MD5;
import com.kingschan.util.web.ServletUtil;
public class DIYTag extends RootTag{
	
	private static final long serialVersionUID = 1L;
	@PropertyField
	@StringField(description="类名")
	private String classname;
	@PropertyField
	@StringField(description="方法名  必须为静态方法public static [void|String] 方法名 (HttpServletRequest req,HttpServletResponse resp)")
	private String method;
	private EasyTagContainer container=EasyTagContainerImpl.getInstance();
	
	@Override
	public int doStartTag() throws JspException {
		if (this.getParent() instanceof GridTag) {
			GridTag gt = (GridTag) this.getParent();
			String filename=gt.getId()+ServletUtil.getRequestURI(gt.getPagecontent())+this.getClassname()+this.getMethod();
			filename=new MD5().getMD5ofStr(filename);
			String path=container.getSerializationPath().concat(filename.concat(".data"));
			gt.addQueryParams("diy", filename);
			try {
				if(!new File(path).exists()){
					BeanUtil.serializationObject(path, this);			
				}
			} catch (IOException e) {		
				e.printStackTrace();
			}
			
		}
		return super.doStartTag();
	}
	
	
	public String getClassname() {
		return classname;
	}
	public void setClassname(String classname) {
		this.classname = classname;
	}
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	
	
	
	
	
}
